package saeildamil.arraylist;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import saeildamil.common.Manager;
import saeildamil.common.Student;

public class ArrayListStdMgn implements Manager {
	ArrayList<Student> stdList;
	File file;

	public ArrayListStdMgn() {
		file = new File("Student.dat");
		init();

	}

	@Override
	public void init() {
		if (!file.exists()) { // 파일이 존재하지않는다면.
			stdList = new ArrayList<>();
		} else {
			inputFile();
		}
		showMenu();
	}

	private void showMenu() {
		int num = 0;
		String message = "수행할 항목의 번호를 입력[1.입력,2.삭제,3.수정,4.검색,5.출력,6.종료]";
		do {
			num = Integer.parseInt(JOptionPane.showInputDialog(message).trim());
			switch (num) {
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			default:

				break;
			}

		} while (num != 6);

	}

	@Override
	public void insertStd() {

	}

	@Override
	public void deleteStd() {

	}

	@Override
	public void updateStd() {

	}

	@Override
	public Object searchStd() {

		return null;
	}

	@Override
	public void printList() {

	}

	@Override
	public void outputFile() {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			for (int i = 0; i < stdList.size(); i++) {
				oos.writeObject(stdList.get(i));
			}
		} catch (FileNotFoundException e) {
			System.out.println("파일못찾음");
		} catch (EOFException e) {
			System.out.println("파일의 끝");
		} catch (IOException e) {
			System.out.println("파일을 못씀");
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				System.out.println("파일을 닫을 수 없음");
			}
		}

	}

	@Override
	public void inputFile() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));
			while (true) {
				Student std = (Student) ois.readObject();
				stdList.add(std);
			}
		} catch (FileNotFoundException e) {
			System.out.println("파일못찾음");
		} catch (EOFException e) {
			System.out.println("파일의 끝");
		} catch (IOException e) {
			System.out.println("파일을 못씀");
		} catch (ClassNotFoundException e) {
			System.out.println("타입이 다름");
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				System.out.println("파일을 닫을 수 없음");
			}
		}

	}

}
